
function initDb() {
    if (!window.indexedDB) {
        console.log("Your browser doesn't support a stable version of IndexedDB.")
        return;
    }
    let request = window.indexedDB.open('modelDB', 1);
    return new Promise((resolve, reject) => {
        request.onerror = function () {
            reject(null)
        };
        request.onupgradeneeded = function (evt) {
            let db = evt.target.result;
            let objectStore = db.createObjectStore('modelTable', {
                keyPath: "id",
                autoIncrement: true
            });
            objectStore.createIndex("INDEX_URL", "url", { unique: true });
        };
        request.onsuccess = function (evt) {
            resolve(evt.target.result)
        };
    })
}
export async function getDatasByIndex(url, v = 1) {
    let db = await initDb();
    let store = db.transaction(['modelTable'], "readwrite").objectStore('modelTable');
    let getRequest = store.index('INDEX_URL').get(url);
    return new Promise((resolve, reject) => {
        getRequest.onsuccess = function (event) {
            let result = event.target.result;
            if (result) {
                if (result.version == v) {
                    db.close();
                    resolve(result);
                } else {
                    let deleteRequest = store.delete(result.id);
                    deleteRequest.onsuccess = function () {
                        console.log("数据删除成功");
                        addData(db, url, v).then((res) => {
                            db.close();
                            resolve(res);
                        }).catch(() => {
                            db.close();
                            reject(null);
                        });
                    };
                    deleteRequest.onerror = function (evt) {
                        console.log("数据删除失败");
                        db.close();
                        reject(null);
                    };
                }
            } else {
                addData(db, url, v).then((res) => {
                    db.close();
                    resolve(res);
                }).catch(() => {
                    db.close();
                    reject(null);
                });
            }
        };
        getRequest.onerror = function (event) {
            console.log('error', event);
            db.close();
            reject(null)
        }
    })
}
async function addData(db, url, v = 1) {
    let res = await downLoadData(url);
    let obj = {
        url: url,
        value: res,
        version: v
    };
    let addRequest = db
        .transaction(['modelTable'], "readwrite")
        .objectStore('modelTable')
        .add(obj);
    return new Promise((resolve, reject) => {
        addRequest.onsuccess = function () {
            console.log("数据添加成功");
            resolve(obj)
        };
        addRequest.onerror = function (evt) {
            console.log("数据添加失败");
            reject(null)
        };
    })
}
function downLoadData(url) {
    return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();
        xhr.responseType = "arraybuffer";
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                var status = xhr.status;
                if (status >= 200 && status < 300) {
                    resolve(xhr.response);
                } else {
                    reject(null);
                }
            }
        };
        xhr.open("GET", url, true);
        xhr.send(null);
    })
}